// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Sprite = cc.Sprite;
import Tool from "../../framework/Tool";
import gameData from "../gameData";

const {ccclass, property} = cc._decorator;

@ccclass
export default class endlessScene3 extends cc.Component {

    @property(cc.Node)
    rotateNode : cc.Node[] = [];

    @property(cc.Node)
    tableNode : cc.Node = null;

    @property(cc.Node)
    roleNode : cc.Node = null;

    @property(cc.Node)
    circleNode : cc.Node[] = [];

    @property(cc.SpriteFrame)
    circlePic : cc.SpriteFrame = null;

    @property(cc.Node)
    maskNode : cc.Node = null;

    @property(cc.Node)
    toolNode : cc.Node[] = [];

    @property(cc.Node)
    waterNode : cc.Node = null;

    @property(cc.Node)
    titleNode : cc.Node = null;

    @property({type : cc.Component.EventHandler,displayName : "触发事件"})
    toolEvent : cc.Component.EventHandler[] = [];

    // LIFE-CYCLE CALLBACKS:

    start () {

        cc.tween(this.titleNode)
            .to(0.2,{scale : 1})
            .delay(2)
            .call(()=>{
                this.titleNode.active = false;
            })
            .start();

        if(gameData.colorfulPiece[5][0] == 1){
            this.toolNode[0].active = false;
        }
        if(gameData.colorfulPiece[5][1] == 1){
            this.toolNode[1].active = false;
        }

        cc.game.on("showView",(res)=>{
            console.log("res",res);
            Tool.loadPrefab("mainPrefab/easterEggView",(data)=>{
                let view = cc.instantiate(data);
                view.getComponent("easterEggWindow").setShowInit(2,()=>{
                    this.toolNode[res - 1].active = false;
                },res);
                view.parent = cc.find("Canvas");
            })
        })
    }

    protected onDestroy(): void {
        cc.game.off("showView");
    }

    isTouchBtn = false;
    rotateSpeed = 3;

    btnTouchEvent(event){
        this.isTouchBtn = !this.isTouchBtn;
    }

    isShowMask = false;
    showMaskNode(){
        if(!this.isShowMask){
            this.isShowMask = true;
            this.maskNode.active = false;
        }else{
            this.isShowMask = false;
            this.maskNode.active = true;
        }
    }


    cameraMoveAction(event){
        if(event[2] == 1){
            if(event[1].y <= event[0].y){
                cc.Camera.main.node.y = 750;
            }else{
                cc.Camera.main.node.y = 0
            }
        }else if(event[2] == 2){
            if(event[1].y <= event[0].y){
                cc.Camera.main.node.y = 0;
            }else{
                cc.Camera.main.node.y = 750;
                let vel = this.roleNode.getComponent(cc.RigidBody).linearVelocity;
                this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(vel.x,vel.y + 600);
            }
        }
    }

    playAnimation(){
        this.waterNode.active = true;
        let spk = this.waterNode.getComponent(sp.Skeleton);
        spk.setCompleteListener(()=>{
            this.waterNode.active = false;
            for(let i in this.circleNode){
                this.circleNode[i].getComponent(Sprite).spriteFrame = this.circlePic;
            }
            this.rotateSpeed = 0.5;
            cc.Component.EventHandler.emitEvents(this.toolEvent, []);
        })
    }

    update (dt) {
        if(this.isTouchBtn){
            this.rotateNode[0].angle += this.rotateSpeed;
            this.rotateNode[1].angle -= this.rotateSpeed;
            this.rotateNode[2].angle += this.rotateSpeed;
            if(this.tableNode.y - this.rotateSpeed >= -40){
                this.tableNode.y -= this.rotateSpeed;
            }
        }else{
            if(this.tableNode.y + this.rotateSpeed <= 106){
                this.tableNode.y += this.rotateSpeed;
            }
        }
    }
}
